<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package    Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since      Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * @see Hmf_Logs
 */    
require_once 'Hmf/Logs.php';
    
/**
 * @see Zend_Registry
 */
require_once 'Zend/Registry.php';        
    
/**
 * Logs
 *
 * @package     Hoffman
 * @subpackage  Hmf
 * @category    Logs
 * @author      Ford AntiTrust
 * @version     SVN: $Id: Logs.php 167 2010-02-18 10:32:16Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */
   
class Hmf_Logs {

    public static function getInstance($fileName = 'logs', $type = 'text') {

        if ($type == 'text') {
            $format = "%pid% \t %sid% \t %timestamp% \t %priorityName% (%priority%)\t %message%" . PHP_EOL;
            
            require_once 'Zend/Log/Formatter/Simple.php';
            $formatter = new Zend_Log_Formatter_Simple($format);
            
            $pathLogFile = PATH_DIR_LOGS . '/' . $fileName . '.log';
        } else {
            $format = array('processid' => 'pid', 'sessionid' => 'sid', 'timestamp' => 'timestamp', 'level' => 'priorityName', 'msg' => 'message');
            
            require_once 'Zend/Log/Formatter/Xml.php';
            $formatter = new Zend_Log_Formatter_Xml('log', $format);
            
            $pathLogFile = PATH_DIR_LOGS . '/' . $fileName . '.xml';
        }
        
        require_once 'Zend/Log/Writer/Stream.php';
        $writer = new Zend_Log_Writer_Stream($pathLogFile);
        
        require_once 'Zend/Log.php';
        $logger = new Zend_Log();
        
        $logger->setEventItem('pid', getmypid());
        $logger->setEventItem('sid', session_id());
        
        $writer->setFormatter($formatter);
        $logger->addWriter($writer);
        
        return $logger;
    }

    public static function sendErrorToLogs($message, $fileName = 'logs') {

        self::getInstance($fileName)->err($message);
    }

    public static function sendInfoToLogs($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.info']) {
            self::getInstance($fileName)->info($message, Zend_Log::INFO);
        }
    }

    public static function sendWarningToLog($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.warning']) {
            self::getInstance($fileName)->warn($message);
        }
    }

    public static function sendNoticeToLog($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.notice']) {
            self::getInstance($fileName)->notice($message);
        }
    }

    public static function sendCriticalToLog($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.critical']) {
            self::getInstance($fileName)->crit($message);
        }
    }

    public static function sendAlertToLog($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.alert']) {
            self::getInstance($fileName)->alert($message);
        }
    }

    public static function sendDebugToLog($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.debug']) {
            self::getInstance($fileName)->debug($message);
        }
    }

    public static function sendEmergencyToLog($message, $fileName = 'logs') {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.emergency']) {
            self::getInstance($fileName)->emerg($message);
        }
    }

    public static function memUsage($controllerName, $actionName, $args = null) {

        $app_config = Zend_Registry::get('app_config');
        
        if ($app_config['logs.memuse']) {
            
            define("MEMORY_PEAK_USAGE", 'Memory peak usage ' . number_format(memory_get_peak_usage() / 1024 / 1024, 4, ".", ",") . ' MByte(s)');
            define("TIMER_CREATED_PAGE", round(array_sum(explode(chr(32), microtime())) - TIMER_CREATE_PAGE, 8));
            
            $msg = '' . $controllerName . '->' . $actionName . '(' . (is_null($args) ? null : implode(', ', $args)) . ')';
            Hmf_Logs::sendInfoToLogs($msg);
            
            $msg = 'Created page in ' . TIMER_CREATED_PAGE . ' secords' . '. ' . MEMORY_PEAK_USAGE;
            Hmf_Logs::sendInfoToLogs($msg);
        }
    
    }
}
?>